package com.bangdream.michelia.view.fragment.main.task;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.contract.TrainPlanContract;
import com.bangdream.michelia.entity.CommentBean;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.entity.RxEventBean;
import com.bangdream.michelia.entity.TaskCurriculumBean;
import com.bangdream.michelia.entity.TrainPlanBean;
import com.bangdream.michelia.entity.TrainPlanDetail;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.presenter.TrainPlanPresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.utils.RxBus;
import com.bangdream.michelia.view.activity.curriculum.CurriculumDetails;
import com.bangdream.michelia.view.activity.curriculum.TrainPlanDetailActivity;
import com.bangdream.michelia.view.adapter.TaskCurriculumAdapter;
import com.bangdream.michelia.view.adapter.TrainPlanAdapter;
import com.bangdream.michelia.view.adapter.TrainPlanNewAdapter;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/6/18 17:13
 * @email 909732184@qq.com
 * 培训计划
 */
public class TaskTrainPlanList extends LazyLoadFragment<TrainPlanContract.ITrainPlanView,TrainPlanPresenter<TrainPlanContract.ITrainPlanView>>
        implements TrainPlanContract.ITrainPlanView{
    private View view;
    //刷新加载控件
    private XRefreshView refreshView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private TrainPlanNewAdapter adapter;
    private int page = 1;
    private List<TrainPlanBean> dataList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.recycler_view_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListener();
        operateBus();
    }

    private void setListener() {

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
                Intent intent = new Intent(getActivity(),TrainPlanDetailActivity.class);
                TrainPlanBean trainPlanBean = adapter.getData().get(position);
                intent.putExtra("trainId",trainPlanBean.getTrainingPlan().getId());
                startActivity(intent);
            }
        });



        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                loadData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                loadData();
            }
        });
    }

    @Override
    public TrainPlanPresenter createPresenter() {
        return new TrainPlanPresenter();
    }

    @Override
    public void loadData() {
        adapter.setEmptyView(loadingView);
        Map params = new HashMap();
        params.put("itemType","05");
        mPresenter.getTrainPlanListData(page, PublicResources.PageSize,params,null);
    }

    @SuppressLint("CheckResult")
    public void operateBus() {
        disposable = RxBus.getInstance().toObservable().map(o -> (RxEventBean) o).subscribe(bean -> {
            if(bean.getCode()==0x02){
                page = 1;
                Map params = new HashMap();
                params.put("itemType","05");
                mPresenter.getTrainPlanListData(page, PublicResources.PageSize,params,null);
            }
        });
    }

    private void initView() {
        refreshView = view.findViewById(R.id.refreshView);
        refreshView.setPullRefreshEnable(true);
        refreshView.setPullLoadEnable(false);
        recyclerView = view.findViewById(R.id.recyclerView);
        initEmptyView(recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new TrainPlanNewAdapter(R.layout.task_trainplan_adapter_item);
        recyclerView.setAdapter(adapter);
    }



    @Override
    public void setTrainPlanList(boolean isSuccess, int page, List<TrainPlanBean> data, String stDescribe) {
        if(isSuccess && data!=null){
            if(page==1) {
                if(data.size()==0){
                    adapter.setNewData(null);
                    adapter.setEmptyView(noDataView);
                }else{
                    dataList = data;
                    adapter.setNewData(dataList);
                    refreshView.stopRefresh();
                    //显示数据大于20条才允许上拉加载更多
                    if(dataList.size()>PublicResources.PageSize){
                        refreshView.setPullLoadEnable(true);
                    }
                }
            }else {
                adapter.addData(data);
                refreshView.stopLoadMore();
            }
        }else {
            //如果是加载新数据时失败了 则page-1
            if (page>1){
                refreshView.stopLoadMore();
                this.page--;
            }else {
                //还是还原为1防止出现负数
                this.page = 1;
                adapter.setNewData(null);
                adapter.setEmptyView(isSuccess?noDataView:errorView);
                refreshView.stopRefresh();
            }
        }
    }

    @Override
    public void setTrainPlanDetails(boolean isSuccess, TrainPlanDetail data) {

    }

    @Override
    public void setTaskCurriculumList(boolean isSuccess, int page, List<TaskCurriculumBean> data, String des) {

    }

}
